import { login, docLogin } from '../../request/index.js';

Page({
  data: {
    id: 0,
    username: '',
    password: '',
    defaultOption:{
      id: 0,
      name: '注册用户'
    },
    options: [
      {
        id: 1,
        name: '管理员'
      }
    ]
  },
  bindPickerChange(e){
    this.setData({
      index: e.detail.value
    });
  },
  close(){
    this.selectComponent('#select').close();
  },
  change(dataset){
    let id = dataset.detail.id;
    this.setData({id: id});
  },
  toRegister(){
    wx.navigateTo({
      url: '../register/register',
    })
  },
  toReset(){
    wx.navigateTo({
      url: '../resetPassword/resetPassword',
    })
  },
  login(){
    if(!this.data.username){
      wx.showToast({
        title: '请输入账号',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    else if(!this.data.password){
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    else{
      let _this = this;
      login({
        id: this.data.username,
        pwd: this.data.password
      })
      .then((res) => {
        if(res.message == 'OK'){
          let app = getApp();
          app.globalData.userId = _this.data.id;
          app.globalData.isLogin = true;
          app.globalData.firstLogin = true;
          app.globalData.order = res.data.order;
          wx.showToast({
            title: '登录成功',
            icon: 'success',
          });
          setTimeout(() => {
            wx.switchTab({
              url: '../index/index',
            });
          },600);
        }
        else{
          wx.showToast({
            title: '账号或密码错误',
            icon: 'error'
          });
        }
      })
    }
  }
})